package Demo38;

/**
 * 消失的两个数字
 * https://leetcode.cn/problems/missing-two-lcci/
 */

class Solution {
    public int[] missingTwo(int[] nums) {
        int tmp = 0;
        // 将所有的数亦或到一起, 就可得到 结果1 和结果2 亦或 的值
        for(int n : nums) tmp ^= n;
        for(int i = 1; i <= nums.length + 2;i++) tmp ^= i;
        // 求出不相同的比特位
        int diff = tmp & (-tmp);
        int[] ret = new int[2];
        for(int n : nums)
            if((n & diff) == 0) ret[0] ^= n;
                else ret[1] ^= n;
        for(int i = 1; i <= nums.length + 2;i++)
            if((i & diff) == 0) ret[0] ^= i;
            else ret[1] ^= i;
        return ret;
    }
}

class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] ret = solution.missingTwo(new int[] {1,3,4});
        for (int n : ret) {
            System.out.print(n  + " ");
        }
    }
}